package com.leetCode.problem;

import java.util.Arrays;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-11-04 13:05:54
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-11-04     张李鑫                     1.0         1.0 Version
 */
public class Problem14 {
    /**
     * 编写一个函数来查找字符串数组中的最长公共前缀。
     */

    public static String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        if (strs.length == 1) {
            return strs[0];
        }
        String target = strs[0];
        int length=target.length();
        for (int i = 1; i < strs.length; i++) {
            int l = 0;
            while (l < length) {
                if ((l < strs[i].length()) && target.charAt(l) == strs[i].charAt(l)) {
                    l++;
                } else {
                    length=Math.min(l,length);
                    break;
                }
            }
        }
        return target.substring(0,length);
    }

    public static void main(String[] args) {
        //输入：strs = ["dog","racecar","car"]
        String[] strs = {"flower","flow","flight"};
        String s = longestCommonPrefix(strs);
        System.out.println(s);

    }
}
